package com.iflytek.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iflytek.entity.Classroom;
import com.iflytek.mapper.ClassroomMapper;
import com.iflytek.query.ClassroomQuery;
import com.iflytek.service.ClassroomService;
import com.iflytek.utils.enums.ResultEnum;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.util.List;

/**
 * @author Administrator
 * @description 针对表【classroom】的数据库操作Service实现
 * @createDate 2022-09-24 14:41:19
 */
@Service
public class ClassroomServiceImpl extends ServiceImpl<ClassroomMapper, Classroom>
        implements ClassroomService {

    @Override
    public PageInfo<Classroom> getAllClassroomList(ClassroomQuery query, Integer pageName, Integer pageSize) {
        if (query == null) {
            return null;
        }
        PageHelper.startPage(pageName, pageSize);
        List<Classroom> classrooms = this.getBaseMapper().getAllClassroomList(query);
        PageInfo<Classroom> pageInfo = new PageInfo<>(classrooms);
        return pageInfo;

    }

    @Override
    public Classroom getClassNo(String classNo) {
        if (StringUtils.isEmpty(classNo)) {
            return null;
        }
        return this.getBaseMapper().selectOne(new QueryWrapper<Classroom>().eq("class_no", classNo));
    }

    @Override
    public Classroom getByName(String name) {
        if (StringUtils.isEmpty(name)) {
            return null;
        }
        return this.getBaseMapper().selectOne(new QueryWrapper<Classroom>().eq("name", name));
    }

    @Override
    public int delBatchByIds(List<String> ids) {
        if (ids == null) {
            return ResultEnum.FAILURE.getCode();
        }
        int del = this.getBaseMapper().deleteBatchIds(ids);
        return del >= 1 ? ResultEnum.SUCCESS.getCode() : ResultEnum.FAILURE.getCode();
    }

    @Override
    public int deleteById(Integer id) {
        if (id == null) {
            return ResultEnum.FAILURE.getCode();
        }
        //检查是否存在，存在就删除
        Classroom classroom = this.getBaseMapper().selectById(id);
        if (classroom == null) {
            return ResultEnum.FAILURE.getCode();
        }
        return this.getBaseMapper().deleteById(id);
    }

    @Override
    public int updateMsgById(Classroom classroom) {
        if (classroom == null) {
            return ResultEnum.FAILURE.getCode();
        }
        //查询信息
        Classroom oldClassroom = this.getBaseMapper().selectById(classroom.getId());
        if (oldClassroom.getId() == null) {
            return ResultEnum.FAILURE.getCode();
        }
        //教室名称是否存在
        if (!oldClassroom.getName().equals(classroom.getName())) {
            Classroom classroom1 = getByName(classroom.getName());
            if (classroom1 != null) {
                return ResultEnum.CLASSNAME_FAILURE.getCode();//表示教室名称存在
            }
        }
        //教室编号是否存在
        if (!oldClassroom.getClassNo().equals(classroom.getClassNo())) {
            Classroom classroom2 = getClassNo(classroom.getClassNo());
            if (classroom2 != null) {
                return ResultEnum.CLASSNO_FAILURE.getCode();//表示教室编号存在
            }
        }

        return this.getBaseMapper().updateById(classroom);
    }

    @Override
    public int saveClassroom(Classroom classroom) {
        if (classroom == null) {
            return ResultEnum.FAILURE.getCode();
        }
        Classroom classroom1 = getByName(classroom.getName());
        if (classroom1 != null) {
            return ResultEnum.CLASSNAME_FAILURE.getCode();//表示教室名称存在
        }
        Classroom classroom2 = getClassNo(classroom.getClassNo());
        if (classroom2 != null) {
            return ResultEnum.CLASSNO_FAILURE.getCode();//表示教室编号存在
        }

        return this.getBaseMapper().insert(classroom);
    }
}




